﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using System.Collections;

namespace GISDeveloping_test3
{
    public partial class LoadLayerDlg : Form
    {
        IWorkspaceFactory pAccessWorkspaceFactory = new FileGDBWorkspaceFactory();
        IFeatureWorkspace pFeatureWorkspace;
        IFeatureLayer pFeatureLayer = new FeatureLayerClass();
        IFeatureDataset pFeatureDataSet;
        IWorkspace pWorkSpace;
        IEnumDataset pEnumDataset;
        IDataset pDataset;
        IDataset pDataset1;
        IEnumDataset pEnumDataset1;
        AxMapControl axMapControl;
        AxTOCControl axTOCControl;
        IRasterWorkspaceEx pRasterWorkSpace;
        string GDBPath;


        public LoadLayerDlg(AxMapControl axMapControl,AxTOCControl axTOCControl)
        {
            InitializeComponent();
            this.axMapControl = axMapControl;
            this.axTOCControl = axTOCControl;
        }



        /// <summary>
        /// 根据要素数据集得到其中的要素类名称列表
        /// </summary>
        /// <param name="featureDataset"></param>
        /// <returns></returns>
        private ArrayList GetFeatureClassNames(FeatureDataset featureDataset)
        {
            IEnumDataset enumDataset = featureDataset.Subsets;
            //遍历每个要素类，判断是否是要素类
            IDataset dataset = enumDataset.Next();
            ArrayList alist = new ArrayList();
            while (dataset != null)
            {
                if (dataset.Type == esriDatasetType.esriDTFeatureClass)
                {
                    alist.Add(dataset.Name);
                    dataset = enumDataset.Next();
                }
            }
            return alist;
        }


        /// <summary>
        /// 根据工作区得到要素数据集名称列表
        /// </summary>
        /// <param name="workspace"></param>
        /// <returns></returns>
        private ArrayList GetDatasetNames(IWorkspace workspace)
        {
            IEnumDatasetName enumDatasetName =
                workspace.DatasetNames[esriDatasetType.esriDTFeatureDataset];
            IDatasetName datasetName = enumDatasetName.Next();
            ArrayList alist = new ArrayList();
            while (datasetName != null)
            {
                alist.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }
            return alist;
        }



        /// <summary>
        /// 根据工作区得到要素类名称列表
        /// </summary>
        /// <param name="workspace"></param>
        /// <returns></returns>
        private ArrayList GetFeatureNames(IWorkspace workspace)
        {
            IEnumDatasetName enumDatasetName =
                workspace.DatasetNames[esriDatasetType.esriDTFeatureClass];
            IDatasetName datasetName = enumDatasetName.Next();
            ArrayList alist = new ArrayList();
            while (datasetName != null)
            {
                alist.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }
            return alist;
        }



        /// <summary>
        /// 根据工作区和要素数据集名称得到要素数据集
        /// </summary>
        /// <param name="sName"></param>
        /// <param name="workspace"></param>
        /// <returns></returns>
        private IFeatureDataset GetFeatureDataset(string sName, IWorkspace workspace)
        {
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = (IFeatureWorkspace)workspace;
            IFeatureDataset featureDataset;
            featureDataset = pFeatureWorkspace.OpenFeatureDataset(sName);
            return featureDataset;
        }



        /// <summary>
        /// 事件回调函数：DatasetList改变时做出相应
        /// 使FeatureList中显示当前选择的Dataset中的要素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DatasetList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DatasetList.SelectedIndex >= 0)
            {
                // 获取选中的数据集名称
                string selectedDatasetName = DatasetList.SelectedItem.ToString();

                // 获取选中的数据集
                IEnumDataset enumDataset = pWorkSpace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                IDataset dataset = enumDataset.Next();

                while (dataset != null)
                {
                    if (dataset.Name == selectedDatasetName)
                    {
                        // 获取数据集中的要素类
                        ListFeatureClasses(dataset as IFeatureDataset);
                        break;
                    }

                    dataset = enumDataset.Next();
                }
            }
        }
        private void ListFeatureClasses(IFeatureDataset featureDataset)
        {
            if (featureDataset == null) return;

            // 清空ListBox2
            FeatureList.Items.Clear();

            // 获取数据集中的要素类
            IEnumDataset enumDataset = featureDataset.Subsets;
            IDataset dataset = enumDataset.Next();

            while (dataset != null)
            {
                // 在ListBox2中添加要素类名称
                FeatureList.Items.Add(dataset.Name);

                dataset = enumDataset.Next();
            }
        }



        /// <summary>
        /// 点击“打开文件”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                GDBPath = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
                pWorkSpace = pAccessWorkspaceFactory.OpenFromFile(GDBPath, 0);
                FilePathLabel.Text = System.IO.Path.GetDirectoryName(openFileDialog.FileName);  //让Label显示地理数据库的路径
                pEnumDataset = pWorkSpace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                pEnumDataset.Reset();
                pDataset = pEnumDataset.Next();
                if (pDataset is IFeatureDataset)
                {
                    pFeatureWorkspace = (IFeatureWorkspace)pWorkSpace;
                }
                pEnumDataset1 = pDataset.Subsets;
                pEnumDataset1.Reset();
                pDataset1 = pEnumDataset1.Next();

                if (pEnumDataset1 is IFeatureClass)
                {
                    pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass =
                        pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                    axMapControl.Map.AddLayer(pFeatureLayer);
                    axMapControl.ActiveView.Refresh();
                }
                else
                {
                    pFeatureWorkspace = (IFeatureWorkspace)pWorkSpace;
                    pFeatureLayer.FeatureClass =
                        pFeatureWorkspace.OpenFeatureClass(pDataset.Name);
                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;


                    //axMapControl.Map.AddLayer(pFeatureLayer);
                    //axMapControl.ActiveView.Refresh();
                }
            }
        }



        /// <summary>
        /// “显示栅格数据”按钮被按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridRB_Click(object sender, EventArgs e)
        {
            if (GridRB.Checked == true)
            {
                if (pWorkSpace == null)
                {
                    return;
                }
                ArrayList datasetArray = GetRasterDatasetNames(pWorkSpace);
                DatasetList.Hide();
                FeatureList.Items.Clear();
                foreach (var item in datasetArray)
                {
                    FeatureList.Items.Add(item);
                }

            }
        }



        /// <summary>
        /// 以RasterWorkspace打开workspace
        /// </summary>
        /// <param name="PGDBPath"></param>
        /// <returns></returns>
        private static IRasterWorkspaceEx OpenRasterWorkspace(string PGDBPath)
        {
            IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactoryClass();
            return (IRasterWorkspaceEx)workspaceFactory.OpenFromFile(PGDBPath, 0);
        }



        /// <summary>
        /// 规定访问地理数据库的栅格数据集时必须使用IRasterWorkspaceEx接口
        /// </summary>
        /// <param name="rasterWorkspaceEx"></param>
        /// <param name="datasetName"></param>
        /// <returns></returns>
        private static IRasterDataset OpenGDBRasterDataset(IRasterWorkspaceEx rasterWorkspaceEx, string datasetName)
        {
            return rasterWorkspaceEx.OpenRasterDataset(datasetName);
        }




        /// <summary>
        /// 从RasterDataset中加载栅格数据
        /// </summary>
        /// <param name="pMap"></param>
        /// <param name="rasterDataset"></param>
        private void LoadRasterLayer(IMap pMap, IRasterDataset rasterDataset)
        {
            IRasterLayer pRasterLayer = new RasterLayerClass();
            pRasterLayer.CreateFromDataset(rasterDataset);
            pMap.AddLayer(pRasterLayer);
        }




        /// <summary>
        /// 根据工作区获取栅格数据集图层列表
        /// </summary>
        /// <param name="workspace"></param>
        /// <returns></returns>
        private ArrayList GetRasterDatasetNames(IWorkspace workspace)
        {
            IEnumDatasetName enumDatasetName =
                workspace.DatasetNames[esriDatasetType.esriDTRasterDataset];
            IDatasetName datasetName = enumDatasetName.Next();
            ArrayList alist = new ArrayList();
            while (datasetName != null)
            {
                alist.Add(datasetName.Name);
                datasetName = enumDatasetName.Next();
            }
            return alist;
        }





        /// <summary>
        /// “显示要素集”按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataSetRB_Click(object sender, EventArgs e)
        {
            if (DataSetRB.Checked == true)
            {
                if (pWorkSpace == null)
                {
                    return;
                }
                ArrayList datasetArray = GetDatasetNames(pWorkSpace);
                DatasetList.Visible = true;
                DatasetList.Items.Clear();
                FeatureList.Items.Clear();
                foreach (var item in datasetArray)
                {
                    DatasetList.Items.Add(item);
                }

            }
        }




        /// <summary>
        /// “显示要素类”按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FeatureRB_Click(object sender, EventArgs e)
        {
            if (FeatureRB.Checked == true)
            {
                if (pWorkSpace == null)
                {
                    return;
                }
                ArrayList datasetArray = GetFeatureNames(pWorkSpace);
                DatasetList.Hide();
                FeatureList.Items.Clear();
                foreach (var item in datasetArray)
                {
                    FeatureList.Items.Add(item);
                }

            }
        }




        /// <summary>
        /// 双击FeatureList中的项，以实现在axMapControl中打开选定的要素类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FeatureList_DoubleClick(object sender, EventArgs e)
        {
            int index = FeatureList.SelectedIndex;
            if (index != ListBox.NoMatches)
            {
                if (GridRB.Checked==true)
                {
                    pRasterWorkSpace = OpenRasterWorkspace(GDBPath);
                    string selectedRasterDatasetName = FeatureList.Items[index].ToString();
                    IRasterLayer rasterLayer = new RasterLayerClass();
                    rasterLayer.CreateFromDataset(OpenGDBRasterDataset(pRasterWorkSpace,selectedRasterDatasetName));
                    rasterLayer.Name = selectedRasterDatasetName;
                    axMapControl.Map.AddLayer(rasterLayer);
                }
                else
                {
                    string selectedFeatureClassName = FeatureList.Items[index].ToString();

                    //将选中的要素类加载到axMapControl中
                    IFeatureLayer featureLayer = new FeatureLayerClass();
                    featureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(selectedFeatureClassName);
                    featureLayer.Name = featureLayer.FeatureClass.AliasName;
                    axMapControl.Map.AddLayer(featureLayer);
                }
                // 获取双击的要素类名称

                axMapControl.ActiveView.Refresh();
            }
        }



        /// <summary>
        /// “确认”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }




        /// <summary>
        /// “取消”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            //IEnumLayer layers= axMapControl.Map.Layers;
            //int count = axMapControl.Map.LayerCount;
            //for (int i = -1; i < count-1; i++)
            //{
            //    ILayer layer = axMapControl.get_Layer(i);
            //    axMapControl.Map.DeleteLayer(layer);
            //}
            axMapControl.Map.ClearLayers();
            axTOCControl.Update();
            axMapControl.Refresh();
            this.Dispose();
        }
    }
}



